package com.zzyl.nursing.service.impl;

import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.dto.AlertDataHandleDto;
import com.zzyl.nursing.dto.AlertDataPageQueryDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;

/**
 * 报警数据Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-28
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据
     *
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id) {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     *
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData) {
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData) {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData) {
        return updateById(alertData) ? 1 : 0;
    }

    /**
     * 批量删除报警数据
     *
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    @Override
    public void handleAlertData(AlertDataHandleDto alertData) {
        // 处理报警数据，根据id获取报警数据，更新处理结果，处理时间
        AlertData alertDataEntity = selectAlertDataById(alertData.getId());
        alertDataEntity.setProcessingResult(alertData.getProcessingResult());
        alertDataEntity.setProcessingTime(alertData.getProcessingTime());
        //设置处理人
        alertDataEntity.setProcessorId(SecurityUtils.getLoginUser().getUser().getUserId());
        //处理人名称
        alertDataEntity.setProcessorName(SecurityUtils.getUsername());
        //设置状态
        alertDataEntity.setStatus(1);
        // 更新报警数据
        updateAlertData(alertDataEntity);
    }

    /**
     *
     * @param alertDataPageQueryDto
     * @return
     */
    @Override
    public Page<AlertData> selectAlertDataListMP(AlertDataPageQueryDto alertDataPageQueryDto) {
        //设置分页信息
        Page<AlertData> page = new Page<>(alertDataPageQueryDto.getPageNum(), alertDataPageQueryDto.getPageSize());
        //设置查询条件
        LambdaQueryWrapper<AlertData> queryWrapper = new LambdaQueryWrapper<>();
        //设备名称精确查询
        if (StrUtil.isNotEmpty(alertDataPageQueryDto.getDeviceName())) {
            queryWrapper.eq(AlertData::getDeviceName, alertDataPageQueryDto.getDeviceName());
        }
        //时间范围筛选
        if (alertDataPageQueryDto.getStartTime() != null && alertDataPageQueryDto.getEndTime() != null) {
            queryWrapper.between(AlertData::getCreateTime, alertDataPageQueryDto.getStartTime(), alertDataPageQueryDto.getEndTime());
        }
        //状态筛选
        if (alertDataPageQueryDto.getStatus() != null) {
            queryWrapper.eq(AlertData::getStatus, alertDataPageQueryDto.getStatus());
        }

        //执行查询
        page = page(page, queryWrapper);
        return  page;
    }

    /**
     * 删除报警数据信息
     *
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id)?1:0;
    }

}
